/*
 * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "app_inc.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*******************************************************************************
 * Variables
 ******************************************************************************/

volatile uint32_t gPintStatus;
volatile uint32_t gPinr;
volatile uint32_t bDirForward = false;

/*******************************************************************************
 * Prototypes
 ******************************************************************************/
void PinDir_Configuration(void);

/*******************************************************************************
 * Code
 ******************************************************************************/
/*!
 * @brief Main function
 */
int main(void)
{
    char ch;

    /* Init board hardware. */
    BOARD_InitPins();
    BOARD_BootClockRUN();

    /* Initialize debug console. */
    BOARD_InitDebugConsole(); /* Flexcomm0 - UART0, 115200. */

    PRINTF("\r\nPINT_EdgeInterrupt Demo.\r\n");
    PRINTF("build time: %s, %s\r\n", __TIME__, __DATE__);

    PinDir_Configuration();

    while (1)
    {
        GETCHAR();
        PRINTF("bDirForward: %d\r\n", bDirForward);
    }
}

#if 0
void PinInterrupt_PIO0_6_BothEdgeTrigger_Callback(void)
{
    if ()
}
#endif

void pint_intr_callback(pint_pin_int_t pintr, uint32_t pmatch_status)
{
    //PRINTF("\f\r\nPINT Pin Interrupt %d event detected.", pintr);

    /* 在双边沿触发模式下，不能确定是上升沿还是下降沿触发，此时在读一下触发引脚的电平就能够确定了 */
    /* 向前：PIO0_6读取低电平 */
    if (0U == ((1U << 6) & GPIO_ReadPinsInput(GPIO, 0U)) )
    {
        bDirForward = true;
    }
    else
    {
        bDirForward = false;
    }
}

void PinDir_Configuration(void)
{
    gpio_pin_config_t GpioPinConfigStruct;

    /* Initialize PINT */
    PINT_Init(PINT); /* 芯片内部就一个PINT模块，所以这里也不用定义映射了 */

    /* Setup Pin Interrupt 0 for rising edge */
    PINT_PinInterruptConfig(PINT, kPINT_PinInt0,
        kPINT_PinIntEnableBothEdges, /* 上下边沿均可触发 */
        pint_intr_callback /* 注册触发时执行的函数 */
        );

    /* 启用GPIO模块，辅助判断边沿信息。
     * 使用PIO0_6
     */
    CLOCK_EnableClock(kCLOCK_Gpio0);
    GpioPinConfigStruct.pinDirection = kGPIO_DigitalInput;
    //GpioPinConfigStruct.outputLogic = 0U;
    GPIO_PinInit(GPIO, 0U, 6U, &GpioPinConfigStruct);

    /* 向前：PIO0_6读取低电平 */
    if (0U == ((1U << 6) & GPIO_ReadPinsInput(GPIO, 0U)) )
    {
        bDirForward = true;
    }
    else
    {
        bDirForward = false;
    }

    /* Enable callbacks for PINT */
    PINT_EnableCallback(PINT);
}


/* EOF. */
